
import { jobGoto } from "../../../job/goto";
import { StructureFixed } from "./fixed";
import { RelevantPlan } from "./structure";

export class StructureCore extends StructureFixed{
    constructor(roomSpawn = undefined, rootPos = undefined){
        if(roomSpawn === undefined){
            super();
        }else{
            super(roomSpawn, rootPos, this.plan());
            this.transfererName = undefined;
            this.exporting = false;
        }
    }

    serialize(){
        return {
            super: super.serialize(),
            transfererName: this.transfererName,
            // terminalId: this.terminalId,
            // storageId: this.storageId,
            // linkId: this.linkId,
            // spawnId: this.spawnId,
        };
    }

    deserialize(obj){
        super.deserialize(obj.super);
        this.transfererName = obj.transfererName;

        let findId = (xOffset, yOffset, STRUCTURE_TYPE)=>
            new RoomPosition(this.rootPos.x+xOffset, this.rootPos.y+yOffset, this.rootPos.roomName)
                .lookFor(LOOK_STRUCTURES)
                .filter((structure)=>structure.structureType === STRUCTURE_TYPE);

        this.terminalId = findId(-1, -1, STRUCTURE_TERMINAL);
        this.storageId  = findId( 1, -1, STRUCTURE_STORAGE);
        this.linkId     = findId(-1,  1, STRUCTURE_LINK);
        this.spawnId    = findId( 1,  1, STRUCTURE_SPAWN);

        return this;
    }
    isDone(){
        return this.mapPlan.isDone();
    }
    plan(_hint = undefined){//todo: might not need it
        const tr = STRUCTURE_TERMINAL;
        const ln = STRUCTURE_LINK;
        const sp = STRUCTURE_SPAWN;
        const st = STRUCTURE_STORAGE;
        const ro = STRUCTURE_ROAD;
        const cr = undefined;
        return new RelevantPlan([
            [tr,ro,st],
            [ro,cr,ro],
            [ln,ro,sp],
        ]);
    }
    get terminal(){
        if(this._terminal !== undefined){
            return this._terminal;
        }
        return this._terminal = Game.getObjectById(this.terminalId);
    }
    get spawnStructure(){
        if(this._spawn !== undefined){
            return this._spawn;
        }
        return this._spawn = Game.getObjectById(this.spawnId);
    }
    get storage(){
        if(this._storage !== undefined){
            return this._storage;
        }
        return this._storage = Game.getObjectById(this.storageId);
    }
    get link(){
        if(this._link !== undefined){
            return this._link;
        }
        return this._link = Game.getObjectById(this.linkId);
    }
    transferThroughLink(link){

    }
    spawn(){
        if(this.transfererName !== undefined){
            return ERR_BUSY;
        }
        let newCreep = spawnCreep(this.spawnRoom, {
            require: [MOVE, CARRY],
            carry: 1.00,
        },{
            memory: {
                role: "structure",
            }
        });
        if(!(newCreep instanceof Creep)){
            return newCreep;
        }
        this.transfererName = newCreep.name;
        return OK;
    }
    run(){
        let creep = Game.creeps[this.transfererName];
        if(!creep){
            return OK;
        }

        if(!creep.pos.isEqualTo(this.rootPos)){
            return jobGoto.run(creep, [{pos: this.rootPos}]);
        }

        let from = [];
        let to = [this.spawnStructure];
        if(this.exporting){

        }else{

        }
    }
}